package com.example.classconnect;

import java.net.MalformedURLException;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.TableOperationCallback;


public class UpdateTable extends Activity {
	private View mLoginFormView;
	private View mLoginStatusView;
	private MobileServiceClient mClient;
	private MobileServiceTable<User> mUserTable;
	private User updateUser;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_table);
		mLoginStatusView = findViewById(R.id.login_status);
		showProgress(true);
		try {
			mClient = new MobileServiceClient(
				      "https://classconnect.azure-mobile.net/",
				      "HzaUWkJBnWTXxCuREwxyQSaxJSyyJm51",
				      this
				);
			// TODO: 3 Get the Mobile Service Table instance to use
			mUserTable = mClient.getTable(User.class);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		updateUser = getIntent().getParcelableExtra("UPDATEUSER");
	    mUserTable.update(updateUser, new TableOperationCallback<User>() {
	        public void onCompleted(User entity, 
	                                Exception exception, 
	                                ServiceFilterResponse response) {
	            if (exception == null) {
	                    System.out.println("Hooray");
	            } 
	        }
	    });
	    Toast.makeText( UpdateTable.this,
				"Profile Updated!!",
				Toast.LENGTH_LONG).show();
	    Intent myIntent = new Intent(UpdateTable.this, MainActivity.class);
		myIntent.putExtra("currentUser", updateUser);
		// *** in the future we want to pass user credentials to next activity
		startActivity(myIntent);
		finish();
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	private void showProgress(final boolean show) {
		// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
		// for very easy animations. If available, use these APIs to fade-in
		// the progress spinner.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			int shortAnimTime = getResources().getInteger(
					android.R.integer.config_shortAnimTime);
			
			mLoginStatusView.setVisibility(View.VISIBLE);
			mLoginStatusView.animate().setDuration(shortAnimTime)
					.alpha(show ? 1 : 0)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(Animator animation) {
							mLoginStatusView.setVisibility(show ? View.VISIBLE
									: View.GONE);
						}
					});
		} else {
			// The ViewPropertyAnimator APIs are not available, so simply show
			// and hide the relevant UI components.
			mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
			mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
		}
	}
}
